package com.js.data.system.controller;


import com.js.data.common.api.CommonResult;
import com.js.data.system.annotation.Log;
import com.js.data.system.dto.DictDTO;
import com.js.data.system.dto.DictItemDTO;
import com.js.data.system.query.DictQuery;
import com.js.data.system.vo.DictVO;
import com.js.data.system.service.DictService;
import com.js.data.system.vo.DictItemVO;
import com.js.data.system.vo.DictPageVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 数据字典 前端控制器
 * </p>
 *
 * @author wujj
 * @since 2022-09-06
 */

@RestController
@RequestMapping("/sys")
public class DictController {

    @Resource
    private DictService dictService;

    /**
     * 系统下字典类型列表
     *
     * @param query 查询条件
     * @return CommonResult<IPage < DictPageVO>>
     */
    @PreAuthorize("@perm.check('system:dict:list')")
    @GetMapping(value = "/dict/page", produces = "application/json; charset=utf-8")
    public CommonResult<IPage<DictPageVO>> pageDict(@ModelAttribute DictQuery query) {
        return CommonResult.success(dictService.pageDict(query));
    }


    /**
     * 新增字典
     *
     * @param dictDTO 数据字段
     * @return void
     */
    @PreAuthorize("@perm.check('system:dict:add')")
    @Log(value = "新增字典")
    @PostMapping(value = "/dict")
    public CommonResult<Void> savDict(@Validated @RequestBody DictDTO dictDTO) {
        dictService.savDict(dictDTO);
        return CommonResult.success();
    }


    /**
     * 修改字典
     *
     * @param dictDTO 数据字段
     * @return void
     */
    @PreAuthorize("@perm.check('system:dict:edit')")
    @Log(value = "修改字典")
    @PutMapping(value = "/dict/{id}")
    public CommonResult<Void> editDict(@PathVariable Long id, @Validated @RequestBody DictDTO dictDTO) {
        dictService.editDict(id, dictDTO);
        return CommonResult.success();
    }

    /**
     * 删除字典
     *
     * @param id 字典id
     * @return void
     */
    @PreAuthorize("@perm.check('system:dict:del')")
    @Log(value = "删除字典")
    @DeleteMapping(value = "/dict/{id}")
    public CommonResult<Void> deleteDict(@PathVariable Long id) {
        dictService.deleteDict(id);
        return CommonResult.success();
    }

    /**
     * 启用数据字典
     *
     * @param id 字典id
     * @return void
     */
    @PreAuthorize("@perm.check('system:dict:state')")
    @Log(value = "启用数据字典")
    @PutMapping(value = "/dict/{id}/state")
    public CommonResult<Void> stateDict(@PathVariable Long id) {
        dictService.stateDict(id);
        return CommonResult.success();
    }

    /**
     * 数据字典项
     *
     * @param id 字典id
     * @return List<DictItemVO>
     */
    @GetMapping(value = "/dict/{id}/item")
    public CommonResult<List<DictItemVO>> listDictItem(@PathVariable Long id) {
        return CommonResult.success(dictService.listDictItem(id));
    }

    /**
     * 保存数据字典项
     *
     * @param id    字典id
     * @param items 数据字典项
     * @return void
     */
    @PreAuthorize("@perm.check('system:dict:add-item')")
    @Log(value = "保存数据字典项")
    @PostMapping(value = "/dict/{id}/item")
    public CommonResult<Void> saveDictItem(@PathVariable Long id, @RequestBody List<DictItemDTO> items) {
        dictService.saveDictItem(id, items);
        return CommonResult.success();
    }

    /**
     * 字典key下字典内容与值
     *
     * @param keyTypes keyTypes
     * @return list
     */
    @GetMapping(value = "/dict/{keyTypes}", produces = "application/json; charset=utf-8")
    public CommonResult<Map<String, List<DictVO>>> listMultipleDict(@PathVariable String keyTypes) {
        return CommonResult.success(dictService.listMultipleDict(keyTypes));
    }
}
